package cn.qm.listener;


import cn.qm.pojo.PayOrder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Map;

@Slf4j
@Component
@RabbitListener
public class RabbitMQListener {

//    @RabbitListener(queues = "simple.queue")
//    public void listenerSimplQueue(String msg){
//        log.info("simple.queue监听到消息:{}",msg);
//    }

    @RabbitListener(queues = "simple.queue")
    public void Work1(String msg) throws InterruptedException {
        log.info("消费者1监听到消息:{}时间{}",msg,LocalDateTime.now());
        Thread.sleep(20);
    }

    @RabbitListener(queues = "simple.queue")
    public void Work2(String msg) throws InterruptedException {
        System.err.println("消费者2监听到消息->"+msg+"时间"+LocalDateTime.now());
        Thread.sleep(20);
    }

    //fanout
    @RabbitListener(queues = "fanout.queue1")
    public void listenerfanoutQueue1(String msg){
        log.info("fanout.queue1监听到消息:{}",msg);
    }

    @RabbitListener(queues = "fanout.queue2")
    public void listenerfanoutQueue2(String msg){
        log.info("fanout.queue2监听到消息:{}",msg);
    }


    //direct
      @RabbitListener(bindings = @QueueBinding(
        value = @Queue(name = "direct.queue1"),
        exchange = @Exchange(name = "itcast.direct",type = ExchangeTypes.DIRECT),
        key = {"red","blue"}
    ))
    public void listenerdirectQueue1(String msg){
        log.info("direct.queue1监听到消息:{}",msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue2"),
            exchange = @Exchange(name = "itcast.direct",type = ExchangeTypes.DIRECT),
            key = {"red","yellow"}
    ))
    public void listenerdirectQueue2(String msg){
        log.info("direct.queue2监听到消息:{}",msg);
    }


    //Topic
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue1"),
            exchange = @Exchange(name = "itcast.topic",type = ExchangeTypes.TOPIC),
            key = "china.#"
    ))
    public void listenerTopicQueue1(String msg){
        log.info("topic.queue1监听到消息:{}",msg);
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue2"),
            exchange = @Exchange(name = "itcast.topic",type = ExchangeTypes.TOPIC),
            key = "#.news"
    ))
    public void listenerTopicQueue2(String msg){
        log.info("topic.queue2监听到消息:{}",msg);
    }


    //消息序列化和反序列化
    @RabbitListener(queues = "object.queue")
    public void listenerObjectQueue(PayOrder payOrder){
        payOrder.setStatus("已支付");
        payOrder.setUpdate_time(LocalDateTime.now());
        log.info("消息序列化和反序列化object.queue监听到消息:{}",payOrder.toString());
    }




}
